from django.db import models
from django.db.models import get_model
from django.contrib.auth.models import User, Permission, Group
from django.contrib import admin
# Create your models here.   
class comments(models.Model):
    order_detail = models.OneToOneField(get_model('orders','order_details'),primary_key=True)
    product = models.ForeignKey(get_model('products','products'),related_name="comments")
    #order = models.ForeignKey(get_model('orders','orders'),related_name="comment_order")
    user = models.ForeignKey(User,related_name="comments_user")
    content = models.TextField()
    created = models.DateTimeField(auto_now=True,auto_now_add=True)
    class Meta:
        ordering = ['-created']
    
    def total_num(self):
        total_num = len(comment_votes.objects.filter(comment=self))
        return total_num
    
    def useful_num(self):
        useful_num = len(comment_votes.objects.filter(comment=self, helpfull_or_not=1))
        return useful_num
    
    def reply_num(self):
        reply_num = len(replys.objects.filter(comment=self))
        return reply_num
    
class comment_votes(models.Model):
    comment = models.ForeignKey(comments,related_name="commentVotes_comments")
    user = models.ForeignKey(User,related_name="commentVotes_user")
    helpfull_or_not = models.IntegerField() #help=1 not=0
class replys(models.Model):
    comment = models.ForeignKey(comments,related_name="replys_comments")
    user = models.ForeignKey(User,related_name="replys_user")
    content = models.TextField()
    created = models.DateTimeField(auto_now=True,auto_now_add=True)
    class Meta:
        ordering = ['created']
admin.site.register(comments)
admin.site.register(replys)
admin.site.register(User)
admin.site.register(Permission)
admin.site.register(Group)